package org.example.backend.utils;

import com.sun.mail.util.MailSSLSocketFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.time.LocalDate;
import java.util.Date;
import java.util.Properties;


/**
 * 发送邮件的工具类
 */
@Component
@Slf4j
public class QQEMailUtils {
    @Value("${spring.mail.username}")
    private   String MY_EMAIL_ACCOUNT;

    @Autowired
    private JavaMailSender mailSender;


    /**
     * 发送应聘申请通过邮件通知
     *
     * @param to               接收邮件的地址
     * @param recruitmentTitle 招聘职位名称
     */
    public void sendApplicationApprovedEmail(String to, String recruitmentTitle) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(MY_EMAIL_ACCOUNT);
            message.setTo(to);
            message.setSubject("应聘申请通过通知");
            message.setText("恭喜您！您对职位 '" + recruitmentTitle + "' 的应聘申请已通过筛选。\n\n" +
                    "我们会尽快与您联系，请保持手机畅通。\n\n" +
                    "祝好！");

            mailSender.send(message);
            log.info("发送邮件成功：{}", to);
        } catch (Exception e) {
            log.error("发送邮件失败：", e);
        }
    }

    /**
     * 发送哔哩哔哩验证码邮件
     *
     * @param to               收件人邮箱地址
     * @param verificationCode 验证码
     */
    public void sendBilibiliVerificationCode(String to, String verificationCode) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

            // 设置发件人（使用哔哩哔哩官方风格）
            helper.setFrom(MY_EMAIL_ACCOUNT, "哔哩哔哩官方");

            // 设置收件人
            helper.setTo(to);

            // 设置主题
            helper.setSubject("【哔哩哔哩】安全验证码");

            // 创建HTML内容（哔哩哔哩风格）
            String htmlContent = "<!DOCTYPE html>"
                    + "<html>"
                    + "<head>"
                    + "    <meta charset='UTF-8'>"
                    + "    <style>"
                    + "        body { font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif; margin: 0; padding: 20px; background-color: #f4f5f7; }"
                    + "        .container { max-width: 600px; margin: 0 auto; background: white; border-radius: 8px; overflow: hidden; box-shadow: 0 2px 12px rgba(0,0,0,0.1); }"
                    + "        .header { background: linear-gradient(135deg, #FB7299 0%, #FF85AD 100%); padding: 30px 20px; text-align: center; }"
                    + "        .logo { width: 60px; height: 60px; }"
                    + "        h1 { color: white; margin: 15px 0 0; font-size: 24px; font-weight: 600; }"
                    + "        .content { padding: 30px; color: #333; line-height: 1.6; }"
                    + "        .code { display: inline-block; padding: 12px 24px; background: #FB7299; color: white; font-size: 24px; font-weight: bold; border-radius: 4px; margin: 15px 0; letter-spacing: 3px; }"
                    + "        .footer { text-align: center; padding: 20px; color: #999; font-size: 12px; background: #f9f9f9; }"
                    + "        .note { background: #f0f7ff; padding: 15px; border-radius: 4px; margin-top: 20px; font-size: 14px; }"
                    + "        .highlight { color: #FB7299; font-weight: 500; }"
                    + "    </style>"
                    + "</head>"
                    + "<body>"
                    + "    <div class='container'>"
                    + "        <div class='header'>"
                    + "            <h1>哔哩哔哩安全验证</h1>"
                    + "        </div>"
                    + "        <div class='content'>"
                    + "            <p>您好！</p>"
                    + "            <p>您正在尝试进行账户安全操作，您的验证码为：</p>"
                    + "            <div class='code'>" + verificationCode + "</div>"
                    + "            <p>请在 <span class='highlight'>5分钟</span> 内完成验证，切勿将验证码泄露给他人。</p>"
                    + "            <div class='note'>"
                    + "                <p>💡 安全提示：</p>"
                    + "                <ul>"
                    + "                    <li>哔哩哔哩官方不会以任何理由索要您的验证码</li>"
                    + "                    <li>如非本人操作，请立即修改账户密码</li>"
                    + "                    <li>有任何疑问请联系客服：support@bilibili.com</li>"
                    + "                </ul>"
                    + "            </div>"
                    + "        </div>"
                    + "        <div class='footer'>"
                    + "            <p>© " + LocalDate.now().getYear() + " 哔哩哔哩 bilibili.com</p>"
                    + "            <p>此邮件由系统自动发送，请勿直接回复</p>"
                    + "        </div>"
                    + "    </div>"
                    + "</body>"
                    + "</html>";

            // 设置HTML内容
            helper.setText(htmlContent, true);

            // 发送邮件
            mailSender.send(message);
            log.info("哔哩哔哩验证码邮件发送成功：{}", to);
        } catch (Exception e) {
            log.error("发送哔哩哔哩验证码邮件失败：", e);
        }
    }
}
